package cn.yangtianxia.org.controller;

import cn.yangtianxia.basic.util.AjaxResult;
import cn.yangtianxia.basic.util.PageList;
import cn.yangtianxia.org.domain.SystemDictionaryType;
import cn.yangtianxia.org.query.SystemDictionaryTypeQuery;
import cn.yangtianxia.org.service.ISystemDictionaryTypeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dictionary")
public class SystemDictionaryTypeController {
    @Autowired
    private ISystemDictionaryTypeService systemDictionaryTypeService;

    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SystemDictionaryType systemDictionaryType){
        try {
            if(systemDictionaryType.getId() != null){
                systemDictionaryTypeService.update(systemDictionaryType);
            }else{
                systemDictionaryTypeService.save(systemDictionaryType);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后重试！！！");
        }
    }

    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable("id") Long id){
        try {
            systemDictionaryTypeService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me() .setSuccess(false) .setMessage("系统繁忙，稍后重试！！！");
        }
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public SystemDictionaryType queryById(@PathVariable("id") Long id){
        return  systemDictionaryTypeService.loadById(id);
    }


    @GetMapping
    public List<SystemDictionaryType> queryAll(){

        List<SystemDictionaryType> systemDictionaryTypes = systemDictionaryTypeService.loadAll();
        return systemDictionaryTypes;
    }

    @PostMapping
    public PageList<SystemDictionaryType> queryPage(@RequestBody SystemDictionaryTypeQuery query){
        return  systemDictionaryTypeService.queryPage(query);
    }

}
